package com.book.service.impl;

import com.book.mapper.ChapterContentDao;
import com.book.mapper.ChapterDao;
import com.book.pojo.Book;
import com.book.mapper.BookDao;
import com.book.pojo.Chapter;
import com.book.pojo.ChapterContent;
import com.book.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.vo.BookVo;
import com.book.vo.ChapterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-10-11 22:05:23
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements BookService {

    @Autowired
    private BookDao bookDao;
    @Autowired
    private ChapterDao chapterDao;
    @Autowired
    private ChapterContentDao chapterContentDao;
    @Override
    public void insertBook(Book book) {
        bookDao.insert(book);
    }


    @Override
    public void updateBook(Book book) {
        bookDao.updateById(book);
    }

    @Override
    public void deleteBook(Long id) {
        bookDao.deleteById(id);
        List<Chapter> chapters = chapterDao.selectChapterByBookId(id);
        Long chapterId = null;
        for (Chapter chapter : chapters) {
             chapterId= chapter.getId();
        }
        chapterDao.deleteById(chapterId);
        chapterContentDao.deleteById(chapterId);
    }

    @Override
    public BookVo getChapterByBookId(Long id) {
        //根据书id找到对应的章节集合
        List<Chapter> chapters = chapterDao.selectChapterByBookId(id);
        //转换
        BookVo bookVo = new BookVo();
        bookVo.setChapters(chapters);
        return bookVo;
    }
}
